package org.dromara.lsc.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.lsc.domain.PreprocessLsc;
import org.dromara.lsc.domain.bo.PreprocessLscBo;
import org.dromara.lsc.domain.vo.PreprocessLscVo;
import org.dromara.lsc.mapper.PreprocessLscMapper;
import org.dromara.lsc.service.IPreprocessLscService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2023-12-11
 */
@RequiredArgsConstructor
@Service
public class PreprocessLscServiceImpl implements IPreprocessLscService {

    private final PreprocessLscMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public PreprocessLscVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<PreprocessLscVo> queryPageList(PreprocessLscBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PreprocessLsc> lqw = buildQueryWrapper(bo);
        Page<PreprocessLscVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<PreprocessLscVo> queryList(PreprocessLscBo bo) {
        LambdaQueryWrapper<PreprocessLsc> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PreprocessLsc> buildQueryWrapper(PreprocessLscBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PreprocessLsc> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMaxRul() != null, PreprocessLsc::getMaxRul, bo.getMaxRul());
        lqw.eq(bo.getSlideWindow() != null, PreprocessLsc::getSlideWindow, bo.getSlideWindow());
        lqw.eq(bo.getStride() != null, PreprocessLsc::getStride, bo.getStride());
        lqw.eq(bo.getSlideType() != null, PreprocessLsc::getSlideType, bo.getSlideType());
        lqw.eq(bo.getNormalType() != null, PreprocessLsc::getNormalType, bo.getNormalType());
        lqw.eq(bo.getNormalBase() != null, PreprocessLsc::getNormalBase, bo.getNormalBase());
        lqw.eq(StringUtils.isNotBlank(bo.getSpilt()), PreprocessLsc::getSpilt, bo.getSpilt());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(PreprocessLscBo bo) {
        PreprocessLsc add = MapstructUtils.convert(bo, PreprocessLsc.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(PreprocessLscBo bo) {
        PreprocessLsc update = MapstructUtils.convert(bo, PreprocessLsc.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PreprocessLsc entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
